$(function(){
	$('#savePayData').bind('click',function(){
		buyerYjfRegist();
	});
})

var buyerYjfRegist = function(){
	//alert(111);return;
	var yjfname = $('#yjfname').val();
	var yjfphone = $('#myPhone').val();
	var userId = localStorage.getItem("userid");
	if(yjfname.trim() ==''){
		alert('请输入用户名.');return;
	}
	if(yjfphone.trim() == ''){
		alert('请输入手机号.');return;
	}
	checkYjfCode(userId,yjfname,yjfphone,commitYjfData);	
}
var commitYjfData = function(userId,yjfname,yjfphone){
	$.ajax({
		url:server_url+'rest/yjf/dealerRegiste.do',
		type:'get',
		data:{userid:userId,username:yjfname,mobile:yjfphone},
		error:function(){
			alert(contants.systemError);
		},
		success:function(msg){
			if(msg.result == 'true'){
				alert('申请成功');
				updateBuyerSupportPayOnline(userId);
				var loop = localStorage.getItem('goPerson');
				if(null != loop){
					window.location.href=server_url+"buyer/product/index.do";
				}else{
					window.location.href=server_url+"buyer/product/addOrder.do";
				}
			}else{
				alert(msg.msg);
			}
		}
	});
}
function checkYjfCode(userId,yjfname,yjfphone,fn) {
	var codeUuid = localStorage.getItem("phoneNoUUID");
	var code = $("input[name='code']").val();
	$.ajax({
		type : "POST",
		dataType : 'json',
		ansyc:false,
		url : server_url + 'rest/frontUser/checkCode.do?',
		data : {
			codeUuid : codeUuid,
			code : code
		},
		success : function(data) {
			//console.log(data);
			if (data == 1) {
				fn(userId,yjfname,yjfphone);
			} else if (data == 0){
			alert("验证码错误");
			}
		}
	});
}
//更新买家是否支持在线支付状态
var updateBuyerSupportPayOnline = function(userId){
	$.ajax({
		url:server_url+'buyer/product/rest/updateSupportPayOnline.do',
		type:'get',
		data:{userId:userId},
		error:function(){
			alert(contants.systemError);
		},
		success:function(msg){
		}
	});
}